package com.bootdo.lgs.ordertemp.controller;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.model.BdConstant;
import com.bootdo.common.utils.R;
import com.bootdo.lgs.combox.service.ComboxService;
import com.bootdo.lgs.order.service.OrderService;
import com.bootdo.lgs.orderbind.model.OrderTempQuery;
import com.bootdo.lgs.ordertemp.model.OrderTemp;
import com.bootdo.lgs.ordertemp.service.OrderTempService;
import com.bootdo.lgs.utils.BeanMapUtils;
import com.bootdo.system.domain.UserDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bootdo 1992lcg@163.com
 */
@Controller
public class OrderTempController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderTempController.class);

    @Autowired
    OrderTempService orderTempService;

    @Autowired
    private ComboxService comboxService;

    @Autowired
    private OrderService orderService;

    //@RequiresPermissions("sys:menu:menu")
    @GetMapping("/lgs/orderTemp/deal")
    String deal(Model model) {
        model.addAttribute("companyCheckBoxList", orderService.getComboxByGrpCd());
        return "lgs/orderTemp/deal";
    }

    @GetMapping("/lgs/orderTemp/mng")
    String mng(Model model) {
        return "lgs/orderTemp/mng";
    }

    @GetMapping("/lgs/orderTemp/mng/showAdd")
    String addTempOrder(Model model) {
        model.addAttribute("companys", comboxService.getComboxByGrpCd(BdConstant.LGS_COMPANY));
        model.addAttribute("transportModes", comboxService.getComboxByGrpCd(BdConstant.TRANSPORT_MODE));
        model.addAttribute("deliveryMethods", comboxService.getComboxByGrpCd(BdConstant.DELIVERY_METHOD));
        return "lgs/orderTemp/add";
    }

    @GetMapping("/lgs/orderTemp/mng/showEdit")
    String updateTempOrder(Model model) {
        model.addAttribute("companys", comboxService.getComboxByGrpCd(BdConstant.LGS_COMPANY));
        model.addAttribute("transportModes", comboxService.getComboxByGrpCd(BdConstant.TRANSPORT_MODE));
        model.addAttribute("deliveryMethods", comboxService.getComboxByGrpCd(BdConstant.DELIVERY_METHOD));
        return "lgs/orderTemp/edit";
    }

    @GetMapping("/lgs/orderTemp/mng/list")
    @ResponseBody
    List<OrderTemp> getList(OrderTempQuery params) {
        return orderTempService.getList(BeanMapUtils.beanToMap(params));
    }

    @PostMapping("/lgs/orderTemp/mng/add")
    @ResponseBody
    R addTemp(@RequestBody OrderTemp condition) {
        UserDO user = getUser();
        if (user == null) {
            return R.error("请先登录！");
        }
        LOGGER.info("Creating temp order by {}", user.getName());

        Map<String, Object> params = BeanMapUtils.beanToMap(condition);
        params.put("record_user_id", user.getUserId());
        return orderTempService.add(params) ? R.ok() : R.error();
    }

    @PostMapping("/lgs/orderTemp/mng/update")
    @ResponseBody
    R update(@RequestBody OrderTemp condition) {
        UserDO user = getUser();
        if (user == null) {
            return R.error("请先登录！");
        }
        LOGGER.info("Updating temp order by {}", user.getName());

        Map<String, Object> params = BeanMapUtils.beanToMap(condition);
        params.put("update_user_id", user.getUserId());
        return orderTempService.update(params) ? R.ok() : R.error();
    }

    @PostMapping("/lgs/orderTemp/mng/updateBatch")
    @ResponseBody
    R updateBatch(@RequestBody List<OrderTemp> conditions) {
        UserDO user = getUser();
        if (user == null) {
            return R.error("请先登录！");
        }
        LOGGER.info("update temp order by {}", user.getName());
        return orderTempService.batchUpdate(BeanMapUtils.objectsToMaps(conditions).stream()
                .peek(item -> item.put("update_user_id", user.getUserId()))
                .collect(Collectors.toList())) ?
                R.ok() : R.error();
    }
}